#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Comm.hpp"
using namespace std;
static int default_cnt = 5;
void useage(string proc)
{
    cout << "Usage:\n\t" << proc << " "
         << "local_port" << endl;
}
bool visitserve(string serverip, uint16_t _port, int *cnt)
{
    ssize_t m = 0;//读
    ssize_t n = 0;//写
    char service_list[1024];
    string inbuffer;
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        cerr << "socket error" << endl;
        return false;
    }
    bool ret = true;
    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(_port);
    inet_pton(AF_INET, serverip.c_str(), &server.sin_addr);
    n = connect(sockfd, CONY(&server), sizeof(server));
    if (n < 0)
    {
        cerr << "connect error" << endl;
        ret = false;
        goto END;
    }
    *cnt = 0;
    m = read(sockfd, service_list, sizeof(service_list)-1);
    cout<<m<<endl;
    if (m > 0)
    {
        service_list[m] = 0;
        cout << "服务器的服务列表是#" << service_list << endl;
    }
    cout << "请你选择服务" << endl;
    getline(cin, inbuffer);
    write(sockfd, inbuffer.c_str(), inbuffer.size());
    cout << "enter>";
    getline(cin, inbuffer);

    if (inbuffer == "quit")
    {
        return true;
    }
    n = write(sockfd, inbuffer.c_str(), inbuffer.size());
    char result[1024];
    if (n > 0)
    {
        result[n] = 0;
        m = read(sockfd, result, sizeof(result)-1);
        if (m > 0)
        {
            result[m] = 0;
            cout << "结果是:" << result << endl;
        }
        else if (m == 0)
        {
            return true;
        }
        else
        {
            ret = false;
            goto END;
        }
    }
    else{
         std::cout << "hello write Error" << std::endl;
        ret=false;
        goto END;
    }
    END:
        close(sockfd);
        return ret;
    }
    // while (true)
    // {
    //     string message;
    //     cout << "please enter#";
    //     getline(cin, message);
    //     ssize_t n = write(sockfd, message.c_str(), message.size());
    //     if (n > 0)
    //     {
    //         char buffer[1024];
    //         ssize_t m = read(sockfd, buffer, sizeof(buffer));
    //         if (m > 0)
    //         {
    //             buffer[m] = 0;
    //             cout << "get a echo message:" << buffer << endl;
    //         }
    //         else if (m == 0 || m < 0)
    //         {
    //             break;
    //         }
    //     }
    //     else
    //     {
    //         break;
    //     }
    // }

int main(int arg, char *argv[])
{
    if (arg != 3)
    {
        useage(argv[0]);
        return 1;
    }
    string serverip = argv[1];
    uint16_t _port = stoi(argv[2]);
    int cnt = 1;
    while (cnt <= default_cnt)
    {
        bool result = visitserve(serverip, _port, &cnt);
        if (result == true)
        {
            break;
        }
        else
        {
            cout << "server offline, retrying..., count : " << cnt << std::endl;
            cnt++;
        }
    }
    if (cnt >= default_cnt)
    {
    std:
        cout << "server offline" << std::endl;
    }

    return 0;
}